package com.lindroid.androiduilearning.expandablelistview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lindroid.androiduilearning.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lin
 * @date 2018/4/27
 * @function 主页选择页面适配器
 */

public class MajorAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<String> groupList = new ArrayList<>();
    private List<List<String>> childList = new ArrayList<>();

    public MajorAdapter(Context mContext, List<String> groupList, List<List<String>> childList) {
        this.mContext = mContext;
        this.groupList = groupList;
        this.childList = childList;
    }

    //获取组选项个数
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //获取子选项个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    //根据position获取组选项数据
    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    //根据position获取子选项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    //获取组选项ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子选项ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //组选项和子选项的ID是否固定
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //获取组选项的视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupViewHolder groupHolder;
        if (convertView == null) {
            groupHolder = new GroupViewHolder();
            convertView = View.inflate(mContext, R.layout.item_list_group, null);
            groupHolder.tvGroup = convertView.findViewById(R.id.tv_group);
            groupHolder.ivIndicator = convertView.findViewById(R.id.iv_indicator);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupViewHolder) convertView.getTag();
        }
        groupHolder.tvGroup.setText((CharSequence) getGroup(groupPosition));
        //根据列表的展开状态来决定箭头的方向
        groupHolder.ivIndicator.setImageResource(isExpanded ?
                R.drawable.ic_arrow_up : R.drawable.ic_arrow_down);
        return convertView;
    }

    //获取组选项的视图
    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childHolder;
        if (convertView == null) {
            childHolder = new ChildViewHolder();
            convertView = View.inflate(mContext, R.layout.item_list_child, null);
            childHolder.tvChild = convertView.findViewById(R.id.tv_child);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }
        childHolder.tvChild.setText((CharSequence) getChild(groupPosition, childPosition));
        return convertView;
    }

    //子选项是否可选
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView tvGroup;
        ImageView ivIndicator;
    }

    class ChildViewHolder {
        TextView tvChild;
    }
}
